package com.lanju.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.lanju.entity.MsgBody;


//@Controller
//@RequestMapping("todo")
public class TodoSomeThingController {
	
	public static void main(String[] args) {
		TodoSomeThingController t = new TodoSomeThingController();
		t.getData();
//		String str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><rtResponse><rtResultList><rtResult><json>{\"data\":{\"isRoam\":\"1\"},\"status\":\"200\"}</json>"+
//"</rtResult></rtResultList><rt_code>200</rt_code><rt_desc>请求成功</rt_desc><rt_flow_id>f0fa9a18-32c9-429f-8030-89e088900b89</rt_flow_id></rtResponse>";
//		System.out.println(str);
//		Map<String,Object> result  = parseXml(str);
//		System.out.println(result);
//		String jsonStr = result.get("json").toString();
//		Object obj = JSON.parse(jsonStr);
//		System.out.println(obj);
//		Map<String,Object> map = (Map<String,Object>)obj;
//		System.out.println(map);
	}
	
	public static Map<String,Object> parseXml(String str){
		Document doc = null;
		Map<String,Object> result = new HashMap<String, Object>();
		  try {
			  doc = DocumentHelper.parseText(str); // 将字符串转为XML
			  Element rootElt = doc.getRootElement(); // 获取根节点
			  Iterator iter = rootElt.elementIterator("rtResultList"); // 获取根节点下的子节点head
			  String rt_code = rootElt.element("rt_code").getTextTrim();
			  String rt_desc = rootElt.element("rt_desc").getTextTrim();
			  String rt_flow_id = rootElt.element("rt_flow_id").getTextTrim();
			  result.put("rt_code", rt_code);
			  result.put("rt_desc", rt_desc);
			  result.put("rt_flow_id", rt_flow_id);
			  String json = rootElt.element("rtResultList").element("rtResult").elementText("json");
			  result.put("json", json);
		} catch (Exception e) {  
			e.printStackTrace();
		}
		return result;
	}
	
	public void getData(){
			MsgBody msgBody = new MsgBody();
	        msgBody.setAccessID("rtroam");
	        msgBody.setAccessPlatformID("111000002");
	        msgBody.setAccessPwd("123456");
	        msgBody.setAccessSEQ("020170525110325111");
	        msgBody.setOperatorID("wap_10010_com_051");
	        msgBody.setOperatorType("search");
	        msgBody.setJson("{\"phone\":\"18617247243\",\"busi\":\"roam\"}");
	        msgBody.setVersion("1.0.0");
	        String body = msgBody.createBody();
	        System.out.println(body);
	        
	        HttpPost post = new HttpPost("http://132.98.26.186:18888/consumer/api/roam/queryRoamForStr");
//	        HttpPost post = new HttpPost("http://localhost:21099/consumer/api/roam/queryRoamForStr");
	        try {
				StringEntity stringEntity = new StringEntity(body);
				stringEntity.setContentEncoding("UTF-8");
				stringEntity.setContentType("application/json");
				post.setEntity(stringEntity);
				HttpClient http = HttpClients.createDefault();
				try {
					HttpResponse response = http.execute(post);
					System.out.println(response.getEntity());
					BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity()  
			                    .getContent()));  
			            StringBuffer sb = new StringBuffer("");  
			            String line = "";  
			            String NL = System.getProperty("line.separator");  
			            while ((line = in.readLine()) != null) {  
			                sb.append(line + NL);  
			            }  
			            in.close();  
			            String content = sb.toString();
			            System.out.println(content);
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
	}
	
	public void mohu(){
		MsgBody msgBody = new MsgBody();
        msgBody.setAccessID("rtroam");
        msgBody.setAccessPlatformID("111000002");
        msgBody.setAccessPwd("123456");
        msgBody.setAccessSEQ("020170525110325111");
        msgBody.setOperatorID("wap_10010_com_051");
        msgBody.setOperatorType("search");
        msgBody.setJson("{\"keyword\":\"86\",\"busi\":\"roam\"}");
        msgBody.setVersion("1.0.0");
        String body = msgBody.createBody();
        System.out.println(body);
        HttpPost post = new HttpPost("http://132.98.23.22:21099/consumer/api/roam/queryBlurRoamForStr");
        try {
			StringEntity stringEntity = new StringEntity(body);
			stringEntity.setContentEncoding("UTF-8");
			stringEntity.setContentType("application/json");
			post.setEntity(stringEntity);
			HttpClient http = HttpClients.createDefault();
			try {
				HttpResponse response = http.execute(post);
				System.out.println(response.getEntity());
				BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity()  
		                    .getContent()));  
		            StringBuffer sb = new StringBuffer("");  
		            String line = "";  
		            String NL = System.getProperty("line.separator");  
		            while ((line = in.readLine()) != null) {  
		                sb.append(line + NL);  
		            }  
		            in.close();  
		            String content = sb.toString();
		            System.out.println(content);
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

}
